/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Util;

/**
 *
 * @author Jack
 */
import java.sql.*;
import Database.ConnectToDatabase;
import java.util.Vector;

public class Customer {

    private int idcustomer;
    private String namecustomer;
    private String sex;
    private String homeaddress;
    private String email;
    private String phone;

    /**
     * @return the idcustomer
     */
    public int getIdcustomer() {
        return idcustomer;
    }

    /**
     * @param idcustomer the idcustomer to set
     */
    public void setIdcustomer(int idcustomer) {
        this.idcustomer = idcustomer;
    }

    /**
     * @return the namecustomer
     */
    public String getNamecustomer() {
        return namecustomer;
    }

    /**
     * @param namecustomer the namecustomer to set
     */
    public void setNamecustomer(String namecustomer) {
        this.namecustomer = namecustomer;
    }

    /**
     * @return the sex
     */
    public String getSex() {
        return sex;
    }

    /**
     * @param sex the sex to set
     */
    public void setSex(String sex) {
        this.sex = sex;
    }

    /**
     * @return the homeaddress
     */
    public String getHomeaddress() {
        return homeaddress;
    }

    /**
     * @param homeaddress the homeaddress to set
     */
    public void setHomeaddress(String homeaddress) {
        this.homeaddress = homeaddress;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the phone
     */
    public String getPhone() {
        return phone;
    }

    /**
     * @param phone the phone to set
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    public static String ListNameCustom(int a) {
        String liste = "";
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select namecustomer from customer,bugreport where bugreport.idcustomer=customer.idcustomer and bugreport.idreport=" + a;
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                liste = rs.getString("namecustomer");
            }
        } catch (Exception ex) {
            liste = null;
        }
        return liste;
    }

    public static Vector<Customer> ListIDE() {
        Vector<Customer> liste = new Vector<Customer>();
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from customer order by idcustomer desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Customer em = new Customer();
                em.setIdcustomer(rs.getInt("idcustomer"));
                em.setNamecustomer(rs.getString("namecustomer"));
                em.setSex(rs.getString("sex"));
                em.setHomeaddress(rs.getString("homeaddress"));
                em.setEmail(rs.getString("emailaddress"));
                em.setPhone(rs.getString("phonenumber"));

                liste.add(em);
            }
        } catch (Exception ex) {
            liste = null;
        }
        return liste;
    }

    public static Customer ListIDEONID(int a) {
        Customer liste = new Customer();
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select order1.idcustomer,namecustomer,sex,homeaddress,emailaddress,phonenumber from customer,order1 where order1.idcustomer=customer.idcustomer and order1.idorder=" + a + " order by idcustomer desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Customer em = new Customer();
                em.setIdcustomer(rs.getInt("idcustomer"));
                em.setNamecustomer(rs.getString("namecustomer"));
                em.setSex(rs.getString("sex"));
                em.setHomeaddress(rs.getString("homeaddress"));
                em.setEmail(rs.getString("emailaddress"));
                em.setPhone(rs.getString("phonenumber"));

                liste = em;
            }
        } catch (Exception ex) {
            liste = null;
        }
        return liste;
    }

    public static int ListIDCustom(int a) {
        int liste = 0;
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select bugreport.idcustomer from customer,bugreport where bugreport.idcustomer=customer.idcustomer and bugreport.idreport=" + a + "order by idcustomer desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                liste = rs.getInt("idcustomer");
            }
        } catch (Exception ex) {
            liste = 0;
        }
        return liste;
    }

    public boolean addcus() {
        return CommonQuery.Command("insert into customer values('" + this.namecustomer + "','" + this.homeaddress + "','" + this.email + "','" + this.phone + "','" + this.sex + "')");

    }

    public boolean updatecus() {
        return CommonQuery.Command("update customer set namecustomer='" + this.namecustomer + "',homeaddress='" + this.homeaddress + "',emailaddress='" + this.email + "',phonenumber='" + this.phone + "',sex='" + this.sex + "' where idcustomer=" + this.idcustomer);
    }

    public boolean deletecus() {
        return CommonQuery.Command("delete customer where idcustomer=" + this.idcustomer);
    }

    public static Vector<Customer> ListIDEONIDCUS(int a) {
        Vector<Customer> liste = new Vector<Customer>();
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from customer where idcustomer=" + a + " order by idcustomer desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Customer em = new Customer();
                em.setIdcustomer(rs.getInt("idcustomer"));
                em.setNamecustomer(rs.getString("namecustomer"));
                em.setSex(rs.getString("sex"));
                em.setHomeaddress(rs.getString("homeaddress"));
                em.setEmail(rs.getString("emailaddress"));
                em.setPhone(rs.getString("phonenumber"));

                liste.add(em);
            }
        } catch (Exception ex) {
            liste = null;
        }
        return liste;
    }

    public static Vector<Customer> ListIDEONNAMECUS(String a) {
        Vector<Customer> liste = new Vector<Customer>();
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from customer where namecustomer like '%" + a + "%'" + " order by idcustomer desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Customer em = new Customer();
                em.setIdcustomer(rs.getInt("idcustomer"));
                em.setNamecustomer(rs.getString("namecustomer"));
                em.setSex(rs.getString("sex"));
                em.setHomeaddress(rs.getString("homeaddress"));
                em.setEmail(rs.getString("emailaddress"));
                em.setPhone(rs.getString("phonenumber"));

                liste.add(em);
            }
        } catch (Exception ex) {
            liste = null;
        }
        return liste;
    }
}
